﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Auction
{
    public partial class FAddWorkToPackage : Form
    {
        private EDM_AuctionContainer context;
        WorkOfArt workFound = null;
        Validations valid = new Validations();
        private Package package;
        
        public FAddWorkToPackage(EDM_AuctionContainer context, Package package)
        {
            InitializeComponent();
            this.context = context;
            this.package = package;        
        }

        private void rdbSearchById_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbSearchById.Checked)
            {
                grb_ID.Enabled = true;
                b_SearchWork.Enabled = true;
            }
            else
            {
                grb_ID.Enabled = false;
            }
        }

        private void rdbSearchByWorkName_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbSearchByWorkName.Checked)
            {
                grb_Name.Enabled = true;
                b_SearchWork.Enabled = true;
            }
            else
            {
                grb_Name.Enabled = false;
            }
        }

        private void b_SearchWork_Click(object sender, EventArgs e)
        {
            lblAdvNotFound.Visible = false;
            workFound = null;
            if (rdbSearchById.Checked)
            {
                int id = valid.GetInt(t_id);
                try
                {
                    workFound = context.WorkOfArtSet.FirstOrDefault(w => w.idWorkOfArt == id);
                    
                  foreach(Package aPackage in context.PackageSet)
                  {
                    foreach (WorkOfArt aWork in aPackage.WorkOfArt)
                    {
                        if (aWork.idWorkOfArt == id)
                        {
                            workFound = null;
                            lblAdvNotFound.Visible = false;
                            l_alreadyAssigned.Visible = true;
                        }
                    }
                  }
                }
                catch (System.Exception)
                {
                    workFound = null;
                    lblAdvNotFound.Visible = true;
                }

            }
            else if (rdbSearchByWorkName.Checked)
            {
                try
                {
                    workFound = context.WorkOfArtSet.FirstOrDefault(w => w.nameOfWork == t_Name.Text.ToString());
                }
                catch (System.Exception)
                {
                    workFound = null;
                }
            }

            if (workFound != null)
            {
                TransferWorkFound(workFound);
            }
            else
            {
                //lblAdvNotFound.Visible = true;
            }
        }
        
        private void LoadDataOfWorkOfArt(WorkOfArt entityWorkOfArt)
        {
            if (entityWorkOfArt != null)
            {

                t_nameAuthor.Text = entityWorkOfArt.AuthorName;
                t_nameWork.Text = entityWorkOfArt.nameOfWork;
                t_surname.Text = entityWorkOfArt.AuthorSurname;
                cb_Type.Text = entityWorkOfArt.typeOfWorkOfArt;
                t_year.Text = entityWorkOfArt.year.ToString();
            }
        }

        public void TransferWorkFound(WorkOfArt aWork)
        {
            this.workFound = aWork;
            b_save.Visible = true;
            gB_Search_Work.Visible = false;
            gB_WorkData.Visible = true;
            LoadDataOfWorkOfArt(aWork);
        }

        private void b_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void b_save_Click(object sender, EventArgs e)
        {
            package.WorkOfArt.Add(workFound);
            context.SaveChanges();
        }    
    }
}
